<template>
  <div class="complete">
    <c-list ref="form" :model="formData" :rules="formRules" label-width="120px">
      <c-list-item label="装修任务号" :value="detail.decorationTaskCode" />
      <c-list-item label="所属门店" :value="detail.shopName" />
      <c-list-item prop="actualEndTime" label="装修完工时间">
        <el-date-picker
          v-model="formData.actualEndTime"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期"
          clearable
        />
      </c-list-item>
    </c-list>
    <RD title="装修图纸信息" :data="drawData" interface-path="/slm-admin/api/v1/shopDesignPicture/getUnConnectPic" :other-filters="otherFilters" @change="changeHandle" />
    <Score v-show="!!detail.constUnitCode" ref="score" :detail="detail" />
    <div class="btn-container">
      <c-button @click="cancelHandle">取消</c-button>
      <c-button type="primary" @click="confirmHandle">确认</c-button>
    </div>
  </div>
</template>
<script>
import { postFinish, postSelectByModelId } from '@/api/decorate'
import { getGetOneByid } from '@/api/decorate'
import { deepClone, otherFiltersItem } from '@/utils'
import RD from '../addEdit/components/rd'
import Score from '../components/score'
export default {
  name: 'Complete',
  components: {
    RD,
    Score
  },
  data() {
    return {
      detail: {},
      formData: {
        actualEndTime: '',
        designPictureDTOS: []
      },
      formRules: {
        actualEndTime: [{ required: true, message: '请选择装修完工时间', trigger: 'change' }]
      },
      drawData: []
    }
  },
  computed: {
    otherFilters() {
      //  1.图纸数据展示范围：所有图纸类型为”施工图“，且图纸进度值为”已完成“的图纸；
      return [
        otherFiltersItem('picType', 'SG', '='),
        otherFiltersItem('schedule', 'F', '='),
        otherFiltersItem('isOutpicUndecoration', '0', '='),
        otherFiltersItem('constCode', this.$route.query.constCode, '=')
      ]
    }
  },
  created() {
    this.getDetail()
    this.getDrawData()
  },
  methods: {
    getDetail() {
      getGetOneByid(this.$route.query.id).then(res => {
        this.detail = res.data
      })
    },
    changeHandle(v) {
      this.formData.designPictureDTOS = v
    },
    confirmHandle() {
      return this.$refs.form.validate().then(() => {
        if (this.formData.designPictureDTOS.length === 0) {
          this.$message.warning('至少包含一个装修图纸信息！')
          return
        }
        const designRange = []
        for (const p of this.formData.designPictureDTOS || []) {
          designRange.push(...this.$split(p.designRange))
        }
        if (!this.$eqSet(this.detail.range, designRange)) {
          this.$message.warning('选择的图纸范围与本次装修范围集合不一致！')
          return
        }
        return this.$refs.score.genData().then(scoreData => {
          const data = deepClone(this.detail)
          data.actualEndTime = this.formData.actualEndTime
          data.designPictureDTOS = this.formData.designPictureDTOS
          data.scoreAndDetailDTOList = scoreData
          return postFinish(data).then(res => {
            this.$message.success('操作成功！')
            this.$store.dispatch('views/goBack')
            this.$emit('on-success')
          })
        })
      })
    },
    getDrawData() {
      postSelectByModelId(this.$route.query.id).then(res => {
        this.drawData = res.data.filter(e => {
          return e.picType === 'SG' && e.schedule === 'F'
        })
      })
    },
    cancelHandle() {
      this.$store.dispatch('views/goBack')
    }
  }
}
</script>
<style lang="scss" scoped>
.complete{
  padding-top: 12px;
}
</style>
